package com.fs.cv.controller;


import com.fs.cv.pojo.pojodao.Course;
import com.fs.cv.pojo.pojodao.Teacher;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.pojo.pojoview.TeacherInfo;
import com.fs.cv.service.TeacherInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.logging.jdbc.PreparedStatementLogger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacherInfo")
public class TeacherInfoController {

    @Autowired
    TeacherInfoService teacherService;


    public User getUser() {
        //获取当前登录的user
        Session session = SecurityUtils.getSubject().getSession();
        User user = (User) session.getAttribute("user");
        return user;
    }

    //功能：查找所有老师
    //参数：无
    //返回结果：所有的老师数据
    @RequestMapping("/queryAllTeacherByUserId")
    @ResponseBody
    public Map<String, Object> queryAllTeacherByUserId(Integer currentPage, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        System.out.println(currentPage+"================");
        System.out.println(pageSize+"===================");
        PageInfo<TeacherInfo> teachers = teacherService.queryAllTeacher(getUser().getUserId(), currentPage, pageSize);
        result.put("teachers",teachers.getList());
        result.put("totalPage",teachers.getPages());
        result.put("code", 0);
        return result;
    }


    //获取当前记录的教师信息
    @RequestMapping("/getOneteacherInfoById")
    @ResponseBody
    public Map<String, Object> getOneteacherInfoById(Integer teacherId) {


        Map<String, Object> result = new HashMap<>();
        Teacher teacher=teacherService.queryTeacherInfoByTeacherId(teacherId);
        result.put("teacher",teacher);//获取当前教师信息
        List<Course> courses = teacherService.querySurplusCourse(getUser().getUserId());//获取当前专业未选择课程
        courses.add(teacherService.queryCourseInfoByCourseId(teacher.getCourseId()));

        result.put("surplusCourse", courses);
        result.put("code", 0);
        return result;
    }

    @RequestMapping("/editTeacherInfo")
    @ResponseBody
    public Integer editTeacherInfo(Teacher teacher) {
        System.out.println("前端返回信息：" + teacher);

        //获取当前教师所教课程ID
        int courseId=teacherService.queryTeacherInfoByTeacherId(teacher.getTeacherId()).getCourseId();

        return teacherService.editTeacherInfo(teacher,courseId);
    }

    @RequestMapping("/deleteOneTeacher")
    @ResponseBody
    public Map<String,Object> deleteTeacher(Integer id){
        Map<String,Object> result=new HashMap<>();
        System.out.println(id);
        boolean flag=teacherService.deleteTeacher(id);
        if(flag){
            result.put("code",0);
            result.put("msg","删除成功");
        }else {
            result.put("code",-1);
            result.put("msg","删除失败");
        }
        return  result;
    }

}
